home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.util.StringTokenizer;
-
- public class Blink extends Applet implements Runnable {
- Thread blinker;
- String lbl;
- Font font;
- int speed;
-
- public void init() {
- this.font = new Font("TimesRoman", 0, 24);
- String att = ((Applet)this).getParameter("speed");
- this.speed = att == null ? 400 : 1000 / Integer.valueOf(att);
- att = ((Applet)this).getParameter("lbl");
- this.lbl = att == null ? "Blink" : att;
- }
-
- public void paint(Graphics g) {
- int x = 0;
- int y = this.font.getSize();
- int red = (int)(Math.random() * (double)50.0F);
- int green = (int)(Math.random() * (double)50.0F);
- int blue = (int)(Math.random() * (double)256.0F);
- Dimension d = ((Component)this).size();
- g.setColor(Color.black);
- g.setFont(this.font);
- FontMetrics fm = g.getFontMetrics();
- int space = fm.stringWidth(" ");
-
- int w;
- for(StringTokenizer t = new StringTokenizer(this.lbl); t.hasMoreTokens(); x += w) {
- String word = t.nextToken();
- w = fm.stringWidth(word) + space;
- if (x + w > d.width) {
- x = 0;
- y += this.font.getSize();
- }
-
- if (Math.random() < (double)0.5F) {
- g.setColor(new Color((red + y * 30) % 256, (green + x / 3) % 256, blue));
- } else {
- g.setColor(Color.lightGray);
- }
-
- g.drawString(word, x, y);
- }
-
- }
-
- public void start() {
- this.blinker = new Thread(this);
- this.blinker.start();
- }
-
- public void stop() {
- this.blinker.stop();
- }
-
- public void run() {
- while(true) {
- try {
- Thread.currentThread();
- Thread.sleep((long)this.speed);
- } catch (InterruptedException var1) {
- }
-
- ((Component)this).repaint();
- }
- }
- }
-